package com.guopiao.vat.cronjob.controller;

import com.github.pagehelper.PageInfo;
import com.guopiao.vat.common.User;
import com.guopiao.vat.common.cronjob.JobAndTrigger;
import com.guopiao.vat.common.general.constants.InvoiceState;
import com.guopiao.vat.common.general.response.GeneralResponse;
import com.guopiao.vat.common.util.ResponseUtil;
import com.guopiao.vat.service.IJobAndTriggerService;
import com.guopiao.vat.service.UserService;
import io.swagger.annotations.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: xander
 * @Date: 2018/7/31 09:55
 * @Description:
 */
@RestController
@Api("swagger2测试controller")
public class Controller {
    @Autowired
    private UserService userService;
    @Autowired
    private IJobAndTriggerService iJobAndTriggerService;

    /**
     * 根据用户名获取用户信息，包括从库的地址信息
     *
     * @param userName
     * @return
     */
    @ApiOperation(value = "根据用户名字查询用户信息",notes = "查询用户信息")
    @ApiImplicitParam(name ="userName",value = "用户名",paramType = "path",required = true,dataType = "String")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",dataType = "String",paramType = "query",example = "xiao")
    })
    @ApiResponses({
            @ApiResponse(code=400,message = "请求参数没有填好"),
            @ApiResponse(code=404,message="请求路径没有找到")
    })
    @GetMapping(value = "/testRest")
    public GeneralResponse findByName(@RequestParam String userName) {
        User user = userService.findByName(userName);
        return ResponseUtil.success(UUID.randomUUID().toString(),InvoiceState.SUCCEED.getCode(),InvoiceState.SUCCEED.getMsg(),user);
    }
    @ApiOperation(value = "测试easypoi导出excel",notes = "测试easypoi导出excel")
    @ApiResponses({
            @ApiResponse(code=400,message = "请求参数没有填好"),
            @ApiResponse(code=404,message="请求路径没有找到")
    })
    @GetMapping(value = "/testRest/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        try {
            List<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetailsByList();
            // 告诉浏览器用什么软件可以打开此文件
            //response.setHeader("content-Type", "application/vnd.ms-excel");
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Calendar calendar = Calendar.getInstance();
            String dateName = df.format(calendar.getTime());
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "No-cache");
            response.setDateHeader("Expires", 0);
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(dateName + ".xls", "UTF-8"));
            ExportParams exportParams=new ExportParams();
            // exportParams.setAddIndex(true);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, JobAndTrigger.class, jobAndTrigger);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            ResponseUtil.error(UUID.randomUUID().toString(),InvoiceState.FAILED.getCode(),e.getMessage());
        }
    }

}
